/**
 * Created by rong on 16/9/5.
 */
require.config(require_config);
define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },
        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'promotion_list',
                condition:{},
                model: {
                    url: _global.url.api + 'promotion_list',
                    event: {
                        '.item-edit:click': 'edit',
                        '.item-remove:click': 'remove',
                    },
                    edit: function (e) {
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        _this.dialog = dialog({
                            title: '编辑活动信息',
                            content: template('add-tpl', this.data),
                            width: 700,
                            ok: function () {
                                _this.data.active_name = $('#active_name').val();
                                if (_this.data.active_name == '') {
                                    Alert({msg: '请输入活动名称', type: 'error'});
                                    return false;
                                }

                                _this.data.type = $("#types").val();
                                if (_this.data.type < 1) {
                                    Alert({msg: '版块不能为空', type: 'error'});
                                    return false;
                                }

                                _this.data.start_time = $('#dt_start').val();
                                _this.data.end_time = $('#dt_end').val();
                                if (_this.data.start_time != '' || _this.data.end_time != '') {
                                    if (_this.data.start_time == '' || _this.data.end_time == '') {
                                        Alert({msg: '开始时间或者结束时间不能为空', type: 'error'});
                                        return false;
                                    }
                                    var date1 = new Date(Date.parse(_this.data.start_time));
                                    var date2 = new Date(Date.parse(_this.data.end_time));
                                    if (date1.getTime() > date2.getTime()) {
                                        Alert({msg: '开始时间不能大于结束时间', type: 'error'});
                                        return false;
                                    }
                                }
                                _this.data.status = $("input[name='status']:checked").val();
                                _this.data.id = _this.data.id;
                                if (model.put()) {
                                    _this.collection.reload();
                                }
                                ;
                            }, cancel: function () {
                            }
                        });
                        _this.dialog.show();
                        $('.long_date').on('click', function () {
                            WdatePicker({
                                el: this.id,
                                dateFmt: 'yyyy-MM-dd HH:mm:ss'
                            });
                        });
                    },
                    remove: function(e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function() {
                                if(model.del()){
                                    _this.collection.reload();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                    },
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },
        event: function () {
            this.data = {};
            var _this = this;
            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.type =$("#type").val();
                _this.collection.condition.start_time = $('input[name="start_time"]').val();
                _this.collection.condition.end_time = $('input[name="end_time"]').val();
                _this.collection.reload();
            });

            $('#btn-status').find('a').on('click', function (e) {
                e.preventDefault();
                $("#type").val('all');
                $('input[name="start_time"]').val('');
                $('input[name="end_time"]').val('');
                _this.collection.condition.type =$("#type").val();
                _this.collection.condition.start_time = $('input[name="start_time"]').val();
                _this.collection.condition.end_time = $('input[name="end_time"]').val();
                _this.collection.condition.status = $(e.target).attr('rel');
                _this.collection.reload();
                $(e.target).addClass('active').siblings().removeClass('active');
            });
            //保存
            $('#btn-add').on('click', function (e) {
                _this.data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    title: '添加活动',
                    content: template('add-tpl', {}),
                    width: 700,
                    ok: function () {
                        _this.data.active_name = $('#active_name').val();
                        if (_this.data.active_name == '') {
                            Alert({msg: '请输入活动名称', type: 'error'});
                            return false;
                        }

                        _this.data.type = $("#types").val();
                        if (_this.data.type < 0) {
                            Alert({msg: '版块不能为空', type: 'error'});
                            return false;
                        }

                        _this.data.start_time = $('#dt_start').val();
                        _this.data.end_time = $('#dt_end').val();
                        if (_this.data.start_time != '' || _this.data.end_time != '') {
                            if (_this.data.start_time == '' || _this.data.end_time == '') {
                                Alert({msg: '开始时间或者结束时间不能为空', type: 'error'});
                                return false;
                            }
                            var date1 = new Date(Date.parse(_this.data.start_time));
                            var date2 = new Date(Date.parse(_this.data.end_time));
                            if (date1.getTime() > date2.getTime()) {
                                Alert({msg: '开始时间不能大于结束时间', type: 'error'});
                                return false;
                            }
                        }
                        _this.data.status = $("input[name='status']:checked").val();
                        if (new Model({
                                url: _global.url.api + 'promotion_add',
                                data: _this.data
                            }).post()) {
                            _this.collection.reload();
                        }
                    }, cancel: function () {
                    }
                });
                _this.dialog.show();
                $('.long_date').on('click', function () {
                    WdatePicker({
                        el: this.id,
                        dateFmt: 'yyyy-MM-dd HH:mm:ss'
                    });
                });
            });
            $('.long_date').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd HH:mm:ss'
                });
            });

        },
    };
    new index();
});